package com.glsc.ngateway.flowable.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Properties;

@Configuration
public class DatasourceConfig {
    private static Logger logger = LoggerFactory.getLogger(DatasourceConfig.class);

    @Resource(name = DataSourceDefConfig.DS_MYSQL)
    private DataSource datasourceMysql;

    @Resource(name = DataSourceDefConfig.DS_MYSQL_GATEWAY)
    private DataSource datasourceMysqlGateway;


    /**
     * 多数据源，自定义mysql/oracle的连接属性
     */
    @Primary
    @Bean(name = "entityManagerFactoryMysql")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryMysql(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean emf = builder
                .dataSource(datasourceMysql)
                .packages("com.glsc.ngateway.common.base.domain.mysql.flowable") //设置实体类所在位置
                .persistenceUnit("mysqlPersistenceUnit")
                .build();
        Properties properties = new Properties();
        //方言
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        //不需要自动创建、更新、验证数据库表结构
        properties.setProperty("hibernate.hbm2ddl.auto", "none");
        properties.setProperty("hibernate.physical_naming_strategy", "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
        //输出SQL
        properties.setProperty("hibernate.show_sql", "false");
        properties.setProperty("hibernate.format_sql", "true");
        //懒加载
        properties.setProperty("hibernate.enable_lazy_load_no_trans", "true");
        emf.setJpaProperties(properties);
        return emf;
    }

    @Bean(name = "entityManagerFactoryMysqlGateway")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryMysqlGateway(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean emf = builder
                .dataSource(datasourceMysqlGateway)
                .packages("com.glsc.ngateway.common.base.domain.mysql.gateway") //设置实体类所在位置
                .persistenceUnit("mysqlGatewayPersistenceUnit")
                .build();
        Properties properties = new Properties();
        //方言
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        //不需要自动创建、更新、验证数据库表结构
        properties.setProperty("hibernate.hbm2ddl.auto", "none");
        properties.setProperty("hibernate.physical_naming_strategy", "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
        //输出SQL
        properties.setProperty("hibernate.show_sql", "false");
        properties.setProperty("hibernate.format_sql", "true");
        //懒加载
        properties.setProperty("hibernate.enable_lazy_load_no_trans", "true");
        emf.setJpaProperties(properties);
        return emf;
    }

    @Primary
    @Bean(name = "entityManagerMysql")
    public EntityManager entityManagerMysql(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryMysql(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerMysqlGateway")
    public EntityManager entityManagerMysqlGateway(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryMysqlGateway(builder).getObject().createEntityManager();
    }


    @Primary
    @Bean(name = "transactionManagerMysql")
    public PlatformTransactionManager transactionManagerMysql(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryMysql(builder).getObject());
    }

    @Bean(name = "transactionManagerMysqlGateway")
    public PlatformTransactionManager transactionManagerMysqlGateway(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryMysqlGateway(builder).getObject());
    }
}
